using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace SerialTool.Models;

// 协议配置基类
[JsonDerivedType(typeof(NoProtocolConfig), typeDiscriminator: "None")]
[JsonDerivedType(typeof(ModbusConfig), typeDiscriminator: "Modbus")]
[JsonDerivedType(typeof(CustomProtocolConfig), typeDiscriminator: "Custom")]
public abstract class ProtocolConfig
{
    public string Name { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public bool IsEnabled { get; set; } = true;
    
    // 协议类型
    public abstract string ProtocolType { get; }
}

// 无协议配置（原始数据传输）
public class NoProtocolConfig : ProtocolConfig
{
    public NoProtocolConfig()
    {
        Name = "无协议";
        Description = "直接发送/接收原始数据，不做协议处理";
    }
    
    public override string ProtocolType => "None";
}

// Modbus协议配置
public class ModbusConfig : ProtocolConfig
{
    public enum ModbusMode { RTU, ASCII }
    
    public ModbusConfig()
    {
        Name = "Modbus";
        Description = "支持Modbus RTU和ASCII模式";
    }
    
    public ModbusMode Mode { get; set; } = ModbusMode.RTU;
    public byte SlaveAddress { get; set; } = 1;
    public bool UseCRC { get; set; } = true;
    public int Timeout { get; set; } = 1000; // 超时时间(ms)
    
    public override string ProtocolType => "Modbus";
}

// 自定义协议配置
public class CustomProtocolConfig : ProtocolConfig
{
    public enum ChecksumMethod { None, Sum, CRC16, XOR, Custom }
    
    public CustomProtocolConfig()
    {
        Name = "自定义协议";
        Description = "自定义数据帧格式和校验方式";
    }
    
    public string StartDelimiter { get; set; } = string.Empty;
    public string EndDelimiter { get; set; } = string.Empty;
    public bool UseChecksum { get; set; } = false;
    public ChecksumMethod Method { get; set; } = ChecksumMethod.None;
    public string CustomChecksumFunction { get; set; } = string.Empty;
    public int PacketTimeout { get; set; } = 100; // 毫秒
    
    public override string ProtocolType => "Custom";
} 